package com.aierkeji.call.cms.service;

import java.util.Date;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.aierkeji.call.cms.contants.Contants;
import com.aierkeji.call.cms.dao.jpa.AkjCallSystemHistoryDao;
import com.aierkeji.call.cms.entity.AkjCallSystemHistory;
import com.aierkeji.call.cms.entity.AkjCallUser;
import com.aierkeji.call.cms.util.DateTimeUtils;
import com.aierkeji.call.cms.util.PageUtils;
import com.aierkeji.call.cms.util.ResultMapHelper;
import com.aierkeji.call.cms.util.StringUtils;

@Service
public class AkjCallSystemHistroyService {
	@Autowired
	AkjCallSystemHistoryDao akjCallSystemHistoryDao;

	public Map<String, Object> list(int pageNo, Map<String, Object> map) {
		// TODO Auto-generated method stub
		Integer pageSize = (Integer) map.get("pageSize");
		String content = (String) map.get("content");
		String startDateStr = (String) map.get("startDate");
		String endDateStr = (String) map.get("endDate");
		Date startDate =DateTimeUtils.converStrToTimestamp("2019-09-01 00:00:00");
		Date endDate = DateTimeUtils.converStrToTimestamp("2199-09-01 23:59:59");
		if (pageSize == null || pageSize <= 0) {
			pageSize = Contants.PAGE_SIZE;
		}
		if (!StringUtils.isEmpty(startDateStr)) {
			startDate = DateTimeUtils.converStrToTimestamp(startDateStr+" 00:00:00");
		}
		if (!StringUtils.isEmpty(endDateStr)) {
			endDate = DateTimeUtils.converStrToTimestamp(endDateStr+" 23:59:59");
		}
		if (!StringUtils.isEmpty(content)) {
			content = "%" + content + "%";
		}else {
			content=null;
		}
		Subject subject = SecurityUtils.getSubject();
		AkjCallUser akjCallUser = (AkjCallUser) subject.getSession().getAttribute("loginUser");
		Pageable pageable = PageUtils.buildPageRequestNoSort(pageNo, pageSize);
		Page<AkjCallSystemHistory> page = null;
		if (akjCallUser.checkRole(Contants.ROLE_ID_MANAGER)) {
			page = akjCallSystemHistoryDao.getList(pageable, akjCallUser.getId(), akjCallUser.getId(), content,
					startDate, endDate);
		} else if (akjCallUser.checkRole(Contants.ROLE_ID_SALES)) {
			page = akjCallSystemHistoryDao.getList(pageable, akjCallUser.getId(), content, startDate, endDate);
		} else {
			page = akjCallSystemHistoryDao.getList(pageable, content, startDate, endDate);
		}

		return ResultMapHelper.success(page);

	}
}
